/*
Copyright (C) 2011 Ricardo Batista <rjbatista at gmail dot com>

This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <TM1638.h>
#include<dht11.h>

// define a module on data pin 8, clock pin 9 and strobe pin 7
TM1638 module(8, 9, 7);
dht11 sensor;

float min=10, max=10;
float u_min=79, u_max=80;
float atual, u_atual;
byte dots = 1;
long value = 0x1234ABCD;

void setup() {
 // initialize serial communication at 9600 bits per second:
 Serial.begin(9600);
 // display a hexadecimal number and set the left 4 dots
 module.setDisplayToHexNumber(0x1234ABCD, 0xF0);

 delay(3000);
}

void loop() {
  Serial.print("Lendo sensor: ");
  int chk = sensor.read(2);
  
  switch(chk) {
    case DHTLIB_OK:
      Serial.println("OK");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Serial.println("Erro no checksum");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.println("Tempo esgotado");
      break;
    default:
      Serial.println("Erro desconhecido");
  }
  
  Serial.print("Umidade atual (%): ");
  Serial.println((float)sensor.humidity, 2);
  Serial.print("Temperatura atual (graus Celsius): ");
  Serial.println((float)sensor.temperature, 2);

  atual = (float)sensor.temperature;
  if(min!=atual && min>atual)
    min = atual;
  if(max!=atual && max<atual)
    max = atual;
    
  u_atual = (float)sensor.humidity;
  if(u_min!=u_atual && u_min>u_atual)
    u_min = u_atual;
  if(u_max!=u_atual && u_max<u_atual)
    u_max = u_atual;
  
  Serial.print("Temperatura minima (graus Celsius): ");
  Serial.println((float)min, 2);
  Serial.print("Temperatura maxima (graus Celsius): ");
  Serial.println((float)max, 2);
  Serial.print("Umidade minima (%): ");
  Serial.println((float)u_min, 2);
  Serial.print("Umidade maxima (%): ");
  Serial.println((float)u_max, 2);
  delay(1000);

 byte keys = module.getButtons();

 Serial.println(dots);

 module.setDisplayToHexNumber((float)sensor.temperature, dots, true);

 // light the first 4 red LEDs and the last 4 green LEDs as the buttons are pressed
 module.setLEDs(((keys & 0xF0) << 8) | (keys & 0xF));

 delay(50);

 dots = (dots * 2);
 if (dots == 0) {
 dots = 1;
 }

 value = (value +1 ) & 0xFFFFFF;
 
}
